
package com.anjiplus.template.gaea.business.modules.httppath.service.impl;

import com.anji.plus.gaea.curd.mapper.GaeaBaseMapper;
import com.anji.plus.gaea.exception.BusinessExceptionBuilder;
import com.anji.plus.gaea.holder.UserContentHolder;
import com.anji.plus.gaea.utils.GaeaBeanUtils;
import com.anjiplus.template.gaea.business.code.ResponseCode;
import com.anjiplus.template.gaea.business.modules.httppath.controller.dto.HttpPathDto;
import com.anjiplus.template.gaea.business.modules.httppath.dao.HttpPathMapper;
import com.anjiplus.template.gaea.business.modules.httppath.dao.entity.HttpPath;
import com.anjiplus.template.gaea.business.modules.httppath.service.HttpPathService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;

/**
 * @author Raod
 * @desc HttpPath 数据集服务实现
 * @date 2021-03-18 12:11:31.150755900
 **/
@Service
//@RequiredArgsConstructor
@Slf4j
public class HttpPathServiceImpl implements HttpPathService {

    @Autowired
    private HttpPathMapper httpPathMapper;


    @Override
    public GaeaBaseMapper<HttpPath> getMapper() {
        return httpPathMapper;
    }

    /**
     * 单条详情
     *
     * @param id
     * @return
     */
    @Override
    public HttpPathDto getById(String id) {
        HttpPathDto dto = new HttpPathDto();
        HttpPath result = selectOne(id);
        if (null == result) {
            return null;
        }
        GaeaBeanUtils.copyAndFormatter(result, dto);
        return getDetailSet(dto);
    }

    @Override
    public List<HttpPath> queryAllHttpPaths() {

        return httpPathMapper.selectList(null);
    }


    public HttpPathDto getDetailSet(HttpPathDto dto) {
        //查询参数

        return dto;
    }


    /**
     * 新增数据集、添加查询参数、数据转换
     *
     * @param dto
     */
    @Override
    @Transactional
    public HttpPathDto insertSet(HttpPathDto dto) {
        String username = UserContentHolder.getContext().getUsername();
        dto.setCreateBy(username);
        dto.setCreateTime(new Date());

        HttpPath dataSet = new HttpPath();
        BeanUtils.copyProperties(dto, dataSet);
        insert(dataSet);
        return dto;
    }

    /**
     * 更新数据集、添加查询参数、数据转换
     *
     * @param dto
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateSet(HttpPathDto dto) {
        if (StringUtils.isEmpty(dto.getId())) {
            throw new RuntimeException("id为空修改失败!");
        }
        String username = UserContentHolder.getContext().getUsername();
        dto.setUpdateBy(username);
        dto.setUpdateTime(new Date());
        //1.更新数据集
        HttpPath dataSet = new HttpPath();
        BeanUtils.copyProperties(dto, dataSet);
        update(dataSet);
    }


    /**
     * 删除数据集、添加查询参数、数据转换
     *
     * @param id
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteSet(String id) {
        HttpPath dataSet = selectOne(id);
        //1.删除数据集
        deleteById(id);

    }

    public void copy(HttpPathDto dto) {
        if (null == dto.getId()) {
            throw BusinessExceptionBuilder.build(ResponseCode.NOT_NULL, "id");
        }
        HttpPath dataSet = selectOne(dto.getId());

        insert(dataSet);
    }


}
